﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;
using BLL;
using Model;

public partial class fileEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Util.UtilWeb.CheckRights(this);
            string strId = Request.QueryString["id"];
            LoadView();
            if (!string.IsNullOrWhiteSpace(strId))
                LoadView(int.Parse(strId));
        }
    }

    private void LoadView()
    {
        var lbl = Master.FindControl("lblTitle") as Label;
        lbl.Text = "文件管理";
        var list = new BllAss().GetList(c => true);
        foreach (var item in list)
        {
            this.ddlAss.Items.Add(new ListItem(item.name, item.id.ToString()));
        }
    }

    private void LoadView(int id)
    {
        hdId.Value = id.ToString();
        btnAdd.Text = "修改";
        BllFile bll = new BllFile();
        var obj = bll.GetSingle(id);
        txtName.Text = obj.name;
        hdSmall.Value = obj.url;
        sc.InnerText = "../" + hdSmall.Value;
        sc.Visible = true;
        hdId.Value = obj.id.ToString();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string cmd = btnAdd.Text.Trim();
        BllFile bll = new BllFile();
        string name = txtName.Text.Trim();
        string ass=ddlAss.SelectedValue;
        bool show = rdoShow.SelectedValue.Equals(0) ? false : true;
        if (name == "")
        {
            Util.UtilWeb.Message(this, Util.KoInfo.FillBlank, null);
            return;
        }
        if (cmd == "添加")
        {
            tb_file obj = new tb_file();
            obj.name = name;
            obj.url = hdSmall.Value;
            obj.name = name;
            obj.show = show;
            obj.time = DateTime.Now;
            obj.ass = ass;
            if (bll.Add(obj))
            {
                Response.Redirect("fileList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
        if (cmd == "修改")
        {
            int id = int.Parse(hdId.Value);
            tb_file obj = bll.GetSingle(id);
            obj.name = name;
            obj.url = hdSmall.Value;
            obj.name = name;
            obj.show = show;
            obj.time = DateTime.Now;
            obj.ass = ass;
            if (bll.Update(obj))
            {
                Response.Redirect("fileList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
    }

    protected void btnUpload1_Click(object sender, EventArgs e)
    {
        string path = Util.UtilWeb.UploadFile(this, fu, null, "../files/");
        hdSmall.Value = path.Replace("../", "");
       
        sc.Visible = true;
    }
}